const app = getApp()
Page({
  data: {
    device_number: "temp", //设备代码
    tempdata: 0, //环境温度
    humidata: 0, //环境湿度
    lightdata: 0, //光照度
    soildata: 0, //土壤湿度
    saltdata: 0, //土壤盐碱度
    stopRequest: false,
    requestInterval: null
  },

  // 读取设备代码
  Read_device_number: function (options) {
    var that = this;
    wx.getStorage({
      key: 'device_number',
      success: function (res) {
        //console.log('读取到的表单数据:', res.data);
        that.setData({
          device_number: res.data
        });
      },
      fail: function (res) {
        console.log('读取表单数据失败');
      }
    });
  },
  // 获取设备信息
  getinfo() {
    var that = this;
    if (this.data.stopRequest) {
      return
    }
    //console.log(this.data.device_number);
    wx.request({
      url: "https://apis.bemfa.com/va/getmsg",
      method: "GET",
      data: {
        "uid": "c124bafd03ba4909b1f7794c3e38af32",
        "topic": this.data.device_number, //设备代码
        "type": "3",
        "num": "1"
      },
      success: function (e) {
        if (!e.data.data || !e.data.data[0] || !e.data.data[0].msg) {
          that.setData({
            stopRequest: true
          });
          clearInterval(that.data.requestInterval);
          wx.showModal({
            title: '错误',
            content: '设备代码输入错误，请检查后重试！',
            showCancel: false,
            confirmText: '确定',
            success: (res) => {
              if (res.confirm) {
                // that.jump_device_login()
                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })
        } else {
          var sentence = e.data.data[0].msg;
          var matches = [];

        }
        var regex = /#([\w\s]+)/g;
        var match;
        while ((match = regex.exec(sentence)) !== null) {
          matches.push(match[1]);
        }
        that.setData({
          tempdata: matches[0],
          humidata: matches[1],
          soildata: matches[2],
          lightdata: matches[3],
          saltdata: matches[4]
        })
        //console.log("temp =", that.data.tempdata)
      },
      fail: function (res) {
        wx.showModal({
          title: '失败',
          content: '请求失败，请检查设备代码或网络连接',
          cancelText: false,
          confirmText: "确定",
          complete: (res) => {
            if (res.confirm) {
              wx.navigateBack({
                delta: 1
              })
            }
          }
        });
      }
    });
  },
  onLoad() {
    var that = this;
    setInterval(function () {
      that.getinfo();
    }, 1000)
  },

  onShow() {
    var that = this;
    this.Read_device_number();
    if (app.globalData.isLogin) {
      this.setData({
        isLogin: true
      })
      this.setData({
        userInfo: app.globalData.userInfo,
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '还未登录，请先登录！',
        showCancel: false,
        success: (res) => {
          if (res.confirm) {
            app.$util.switchTabTo("/pages/user/user");
          }
        }
      })
      this.setData({
        isLogin: false
      })
    }
    if (this.data.isLogin) {
      wx.cloud.callFunction({
        name: "db",
        data: {
          $url: "getUserInfo"
        }
      }).then(res => {
        if (res.result.code) {
          app.$db.set('userInfo', res.result.userInfo);
          this.setData({
            userInfo: res.result.userInfo,
            nickName: res.result.userInfo.nickName
          })
          app.globalData.userInfo = res.result.userInfo
        }
      })
    }
  },
  jump_device_login() {
    wx.navigateTo({
      url: '../device_login/device_login'
    })
  },
})